let startDateInput = $('#startDate');
let createDateInput = $('#createDate');
let testButton = $('#test');
let testToDatabaseButton = $('#testToDatabase');
let queryButton = $('#query');
let queryIdInput = $('#queryId');

let saveButton = $('#save');
let nameInput = $('#name');

function buttonInit() {
    testButton.on("click", function () {
        $.get("/api/java8date", {startDateStr: startDateInput.val()}, function (response) {
            console.log(response);
        });
    });

    testToDatabaseButton.on("click", function () {
        $.get("/api/java8date/db", {startDateStr: startDateInput.val()}, function (response) {
            console.log(response);
        });
    });

    queryButton.on("click", function () {
        $.get("/api/localDate", {id: queryIdInput.val()}, function (response) {
            console.log(response);
        });
    });

    saveButton.on('click', () => {
        if(nameInput.val() === '') {
            swal({
                title: '警告',
                text: 'name不能为空',
                type: 'warning'
            });
            return;
        }
        let createDate = createDateInput.val();
        let data = {
            name: nameInput.val(),
            createDate: createDate === null ? null : createDate + "T00:00:00"
        };
        $.ajax({
            /*设置请求方式*/
            type: "post",
            /*请求路径*/
            url: "/api/localDate",
            /*请求携带的参数, 后台接受时使用了@RequestBody注解，需要Json字符串和指定contentType*/
            data: JSON.stringify(data),
            contentType:"application/json;charset=utf-8",
            /*是否异步*/
            async: true,
            /*返回的数据类型*/
            dataType:"json",//返回数据类型
            /*请求成功的回调函数*/
            success: function(response){
                console.log(response);
            }
        });
    });
}

function datepickerInit() {
    startDateInput.datepicker({
        format: 'yyyy-mm-dd',
        language: "zh-CN",
        todayBtn: 'linked',
        autoclose: true
    });

    createDateInput.datepicker({
        format: 'yyyy-mm-dd',
        language: "zh-CN",
        todayBtn: 'linked',
        autoclose: true
    });
}

$(function () {
    buttonInit();
    datepickerInit();
});